package com.wl.shixun3.dao;

import com.wl.shixun3.entity.User;
import java.sql.*;

public class UserDAO {
    
    // 数据库连接
    protected Connection getConnection() {
        String url = "jdbc:mysql://localhost:3306/db01?useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=UTF-8";
        String user = "root";
        String password = "a15207291068"; // 请根据实际情况修改密码
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            return DriverManager.getConnection(url, user, password);
        } catch (Exception e) {
            throw new RuntimeException("数据库连接失败", e);
        }
    }

    // 把user对象插入users表中
    public boolean insertUser(User user) throws SQLException {
        boolean rowInserted = false;
        String sql = "INSERT INTO users (username, password, security_question, security_answer) VALUES (?, ?, ?, ?)";
        try (Connection connection = getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setString(1, user.getUsername());
            preparedStatement.setString(2, user.getPassword());
            preparedStatement.setString(3, user.getSecurityQuestion());
            preparedStatement.setString(4, user.getSecurityAnswer());
            rowInserted = preparedStatement.executeUpdate() > 0;
        }
        return rowInserted;
    }

    // 根据username从表users获取user对象
    public User selectUser(String username) {
        User user = null;
        String sql = "SELECT * FROM users WHERE username = ?";
        try (Connection connection = getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setString(1, username);
            try (ResultSet resultSet = preparedStatement.executeQuery()) {
                if (resultSet.next()) {
                    user = new User();
                    user.setId(resultSet.getInt("id"));
                    user.setUsername(resultSet.getString("username"));
                    user.setPassword(resultSet.getString("password"));
                    user.setSecurityQuestion(resultSet.getString("security_question"));
                    user.setSecurityAnswer(resultSet.getString("security_answer"));
                }
            }
        } catch (SQLException e) {
            throw new RuntimeException("查询用户失败", e);
        }
        return user;
    }

    // 检查用户是否存在
    public boolean checkIfUserExists(String username) {
        return selectUser(username) != null;
    }
    
    // 更新用户密码
    public boolean updatePassword(String username, String newPassword) {
        boolean updated = false;
        String sql = "UPDATE users SET password = ? WHERE username = ?";
        try (Connection connection = getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setString(1, newPassword);
            preparedStatement.setString(2, username);
            updated = preparedStatement.executeUpdate() > 0;
        } catch (SQLException e) {
            throw new RuntimeException("更新密码失败", e);
        }
        return updated;
    }
}

